package com.fxnotes.book.node;

import com.fxnotes.book.presenter.BookDetailsPresenter;
import com.fxnotes.common.FxUtils;
import com.fxnotes.common.ui.CustomNode;
import com.fxnotes.domain.model.Book;
import com.fxnotes.domain.model.Chapter;
import com.fxnotes.domain.model.Page;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.util.List;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import net.miginfocom.layout.LC;
import org.tbee.javafx.scene.layout.MigPane;

@Singleton
public class BookDetailsNode implements CustomNode<Parent> {

    private BookDetailsPresenter presenter;
    private MigPane pane;
    private Button booksHome;
    private Label bookName;
    private EditEntityComponent<Chapter> chaptersComponent;
    private EditEntityComponent<Page> pagesComponent;

    public BookDetailsNode() {
        initUI();
        initHandlers();
    }

    @Inject
    public void setPresenter(BookDetailsPresenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public Parent getNode() {
        return pane;
    }

    private void initUI() {
        HBox namePanel = new HBox();
        namePanel.setPadding(new Insets(10));
        namePanel.setSpacing(20);

        booksHome = new Button("\u21D0 Books");
        bookName = new Label();
        bookName.setFont(new Font(16));

        EditEntityHandler<Chapter> chaptersHandler = new EditEntityHandler<Chapter>() {

            @Override
            public void onAdd(String name) {
                presenter.addChapter(name);
            }

            @Override
            public void onUpdate(Chapter chapter, String name) {
                presenter.updateChapter(chapter, name);
            }

            @Override
            public void onRemove(Chapter chapter) {
                presenter.removeChapter(chapter);
            }

            @Override
            public void onSelectByClick(Chapter chapter) {
                reloadPages(chapter, false);
            }

            @Override
            public void onChangePosition(List<Chapter> chapters, Chapter changedChapter) {
                presenter.updateChaptersPosition(chapters, changedChapter);
            }
        };
        chaptersComponent = EditEntityComponentBuilder.<Chapter>create()
                .setEntityName("chapter")
                .setHeader("Chapters")
                .setWidth(250)
                .setHeight(350)
                .setMoveButtonsVisible(true)
                .setHandler(chaptersHandler)
                .build();

        EditEntityHandler<Page> pagesHandler = new EditEntityHandler<Page>() {

            @Override
            public void onAdd(String name) {
                presenter.addPage(getSelectedChapter(), name);
            }

            @Override
            public void onUpdate(Page page, String name) {
                presenter.updatePage(getSelectedChapter(), page, name);
            }

            @Override
            public void onRemove(Page page) {
                presenter.removePage(getSelectedChapter(), page);
            }

            @Override
            public void onChangePosition(List<Page> pages, Page changedPage) {
                presenter.updatePagesPosition(getSelectedChapter(), pages, changedPage);
            }

            @Override
            public void onSelectByDoubleClick(Page page) {
                presenter.showPage(getSelectedChapter(), page);
            }

            private Chapter getSelectedChapter() {
                return chaptersComponent.getSelectedItem();
            }
        };
        pagesComponent = EditEntityComponentBuilder.<Page>create()
                .setEntityName("page")
                .setHeader("Pages")
                .setWidth(300)
                .setHeight(350)
                .setMoveButtonsVisible(true)
                .setHandler(pagesHandler)
                .build();

        pane = new MigPane(new LC().align("center", "center"));
        pane.add(booksHome);
        pane.add(bookName, "wrap");
        pane.add(chaptersComponent.getNode(), "spanx 2");
        pane.add(pagesComponent.getNode(), "wrap");
    }

    private void initHandlers() {
        booksHome.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                presenter.showBooks();
            }
        });
    }

    public void reloadBook(Book book) {
        bookName.setText(book.getName());
        reloadChapters(book);
    }

    public void reloadChapters(Book book) {
        chaptersComponent.reload(book.getChapters(), true);
        clearPages();
    }

    public void reloadPages(Chapter chapter, boolean focus) {
        if (chapter != null) {
            pagesComponent.reload(chapter.getPages(), focus);
        }
    }

    public void clearPages() {
        pagesComponent.clear(false);
    }

    public void reportError(String message) {
        FxUtils.displayError(pane.getScene().getWindow(), message);
    }
}
